﻿// Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// added to allow for serialization
using System.Runtime.Serialization;

//------------------------------------------------------------------------------
// this file represents the extensions I added to make the service a little 
// nicer to work with.
// I added a property to all the items that I thought need extra graph serialization
// what I wanted todo is show users how to stream related items.
//------------------------------------------------------------------------------

namespace MyTravelPostModel
{
    // I added a property BlogWriter which allows for the Person
    // entity to be serialized with the blog.
    public partial class Blog
    {
        // The person that created the blog.
        [DataMemberAttribute()]
        public Person BlogWriter
        {
            get
            {
                if (PersonReference.IsLoaded)
                {
                    return Person;
                }
                else
                {
                    return _cachedPerson;
                }
            }
            set
            {
                _cachedPerson = value;
            }
        }

        Person _cachedPerson;
    }

    // The blog post should have the comments associated, most of the 
    // time the user will need this.
    public partial class BlogPost
    {
        // if the blog post has loaded comments, this returns the list
        // and caches the list separatly. The trick happens when the
        // deserialization occurs, that the user needs to merge the
        // two lists together in their own logic.
        [DataMemberAttribute()]
        public List<Comment> PostComments
        {
            get
            {
                // do a check to make sure that the post comments
                // are in sync. This is just for the service.
                if (Comments.IsLoaded)
                {
                    return Comments.ToList();
                }
                else
                {
                    return _cachedComments;
                }
            }
            set
            {
                _cachedComments = value;
            }
        }

        List<Comment> _cachedComments = null;
    }


    // With comments, you need the person writing the comments.
    public partial class Comment
    {
        // The comment writer
        [DataMemberAttribute()]
        public Person CommentWriter
        {
            get
            {
                if (PersonReference.IsLoaded)
                {
                    return Person;
                }
                else
                {
                    return _cachedPerson;
                }
            }
            set
            {
                _cachedPerson = value;
            }
        }

        Person _cachedPerson;
    }
}